<?php

namespace App\Admin\Controllers;

use App\crm_customer;
use App\Admin_users;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Widgets\Chart\Radar;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class Crm_guanhuaiController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('客户关怀');
            $content->description('生日提醒');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('客户管理');
            $content->description('客户详情');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(crm_customer::class, function (Grid $grid) {
            if(Admin::user()->isAdministrator()){
                \DB::table('crm_customer')->where('customer_shengri','!=',date('md'))->where('customer_srxinxi',2)->update(['customer_srxinxi' => 1]);
                $grid->model()->where('customer_shengri',date('md'))->where('customer_srxinxi',1);
            }else{
                \DB::table('crm_customer')->where('customer_shengri','!=',date('md'))->where('customer_srxinxi',2)->update(['customer_srxinxi' => 1]);
                $grid->model()->where('customer_weihu',Admin::user()->id)->where('customer_shengri',date('md'))->where('customer_srxinxi',1);
            }
            $grid->id('序号')->sortable();
            $grid->customer_name('客户姓名');
            $grid->customer_phone('客户电话');
            $grid->customer_type('客户类别');
            $grid->customer_danwei('单位');
            $list=['1'=>'网站','2'=>'转介绍','3'=>'上门','4'=>'电话销售','5'=>'老客户','6'=>'其他'];
            $grid->column('customer_laiyuan','客户来源')->display(function ($customer_laiyuan) use ($list) {
                return $list[$customer_laiyuan];
            });
            $grid->column('admin_users.username','维护人');
            $grid->customer_kfdate('录入时间');
            $grid->disableCreation();       //把新增框干掉
            $grid->filter(function ($filter) {
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->equal('customer_type','客户类别')->select(['单位'=>'单位','个人'=>'个人']);
                $filter->equal('customer_laiyuan','客户来源')->select(['1'=>'网站','2'=>'转介绍','3'=>'上门','4'=>'电话销售','5'=>'老客户','6'=>'其他']);
                $filter->like('customer_name','客户姓名');
                $filter->equal('customer_phone','客户电话');
                $filter->like('customer_danwei','单位名称');
                if(Admin::user()->roles[0]->name=="总经理"){
                    $filter->like('customer_weihu','维护人');
                }
            });
            $grid->actions(function ($actions) {
                $actions->disableDelete();//禁止删除
                $actions->disableEdit();//禁止编辑
                $actions->append('<a class="btn btn-sm btn-primary" href="'.admin_url('xiaoxi/shengri').'/'.$actions->getKey().'">生日提醒</a>');
            });
            $grid->disableExport();//禁用导出数据按钮
            $grid->disableRowSelector();//禁止行操作

        });
    }

   
}
